package com.autumn.zero.workflow.web.controllers;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import com.autumn.common.api.application.dto.DefaultEntityDto;
import com.autumn.common.api.application.dto.input.AdvancedPageQueryInput;
import com.autumn.common.api.application.dto.input.AdvancedQueryInput;
import com.autumn.common.api.application.dto.input.DefaultStatusInput;
import com.autumn.common.api.application.dto.output.IPageQueryResult;
import com.autumn.security.constants.AuthorizationConstants;
import com.autumn.web.annotation.IgnoreApiResponseBody;
import com.autumn.zero.workflow.application.dto.forms.WorkflowFormDetailsOutput;
import com.autumn.zero.workflow.application.dto.forms.WorkflowFormInput;
import com.autumn.zero.workflow.application.dto.forms.WorkflowFormOutput;
import com.autumn.zero.workflow.application.services.IWorkflowFormAppService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 工作流表单控制器
 * 
 * @author 杨昌国 2018-12-20 11:15:07
 */
@RestController
@RequestMapping("/sys/workflow/form")
@Api(tags = "工作流表单")
public class WorkflowFormController {

	@Autowired
	private IWorkflowFormAppService service;

	/**
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加表单")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public WorkflowFormDetailsOutput add(@Valid @RequestBody WorkflowFormInput input) {
		return service.add(input);
	}

	/**
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改表单")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public WorkflowFormDetailsOutput update(@Valid @RequestBody WorkflowFormInput input) {
		return service.update(input);
	}

	/**
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/deleteById")
	@ApiOperation(value = "删除表单")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public void deleteById(@Valid @RequestBody DefaultEntityDto input) {
		service.deleteById(input.getId());
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/queryById")
	@ApiOperation(value = "获取表单")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public WorkflowFormDetailsOutput queryById(@Valid @RequestBody DefaultEntityDto input) {
		return service.queryById(input.getId());
	}

	/**
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/queryByList")
	@ApiOperation(value = "查询表单列表")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public List<WorkflowFormOutput> queryByList(@Valid @RequestBody AdvancedQueryInput input) {
		return service.queryByList(input);
	}

	/**
	 * 
	 * @param input
	 * @return
	 */
	@PostMapping("/queryByPageList")
	@ApiOperation(value = "分页查询表单")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public IPageQueryResult<WorkflowFormOutput> queryByPageList(@Valid @RequestBody AdvancedPageQueryInput input) {
		return service.queryByPageList(input);
	}

	/**
	 * 更新状态
	 * 
	 * @param input 输入
	 * @return
	 */
	@PostMapping("/updateByStatus")
	@ApiOperation(value = "更新状态")
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public void updateByStatus(@Valid @RequestBody DefaultStatusInput input) {
		service.updateByStatus(input);
	}

	/**
	 * 
	 * @param input
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping("/downloadByExcel")
	@ApiOperation(value = "下载Excel", produces = "application/octet-stream")
	@IgnoreApiResponseBody
	@RequiresRoles(value = { AuthorizationConstants.SYSTEM_ROLE_SYSTEMS,
			AuthorizationConstants.SYSTEM_ROLE_ADMINISTRATORS }, logical = Logical.OR)
	public StreamingResponseBody downloadByExcel(@Valid @RequestBody AdvancedQueryInput input,
			HttpServletResponse response) throws UnsupportedEncodingException {
		String fileName = URLEncoder.encode(service.getModuleName() + ".xls", "utf-8");
		response.setContentType("application/xls;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		Workbook workbook = service.exportByExcel(input);
		return outputStream -> {
			try {
				workbook.write(outputStream);
			} finally {
				outputStream.flush();
				outputStream.close();
			}
		};
	}
}
